<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller;

use App\Model\Data\GoodsData;
use Swoft;
use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Response;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\View\Renderer;
use Throwable;
use function bean;
use function context;
use App\Model\Entity\Emp;
use App\Model\Entity\UserInfo;
use Swoft\Redis\Pool;
use Swoft\Redis\Redis;
use Swoft\Bean\Annotation\Mapping\Inject;
use App\Http\Middleware\TestMiddleware;
use Swoft\Http\Server\Annotation\Mapping\Middleware;
use App\Exception\ApiException;
use Swoft\Db\DB;

/**
 * Class WsqeController
 * @Controller(prefix="/wsq")
 */
class WsqController
{
    /**
     * @Inject()
     *
     * @var Pool
     */
    private $redis;

	/**
     * @RequestMapping(route="list")
     * http://127.0.0.1:18306/wsq/list
     */
    public function list(){
    	return context()->getResponse()->withContent('wwssqq');
    }

    /**
     * @RequestMapping(route="info")
     * http://127.0.0.1:18306/wsq/info/?name=www
     * 参数传递
     */
    public function info(Request $request){
    	$name = $request->input('name');
    	return context()->getResponse()->withContent($name);//输出字符串
    }


    /**
     * @RequestMapping(route="jsondata")
     * http://127.0.0.1:18306/wsq/jsondata/?age=34
     * 参数传递
     */
    public function jsondata(Request $request,Response $response){
    	$data = $request->input();
    	return $response->withData($data);
    }


    /**
     * @RequestMapping(route="add")
     * http://127.0.0.1:18306/wsq/mysqldata/
     * 数据库添加数据
     */
    public function add(Response $response){
    	$_data = [
    		'ename'=>'hello',
    		'hiredate'=>'2020-03-13',
    		'sal'=>'888',
    		'deptno'=>'22',
    		'tin'=>'0'
    	];
    	$_model = Emp::new($_data);
    	$_model->save();
    	return $response->withData(['code'=>1]);
    }


    /**
     * @RequestMapping(route="update")
     * http://127.0.0.1:18306/wsq/mysqldata/
     * 数据库添加数据
     */
    public function update(Response $response){
    	$_data = [
    		'ename'=>'王松青heihei',
    	];
    	$id = '5';
    	$_model = Emp::where(['id'=>$id])->update($_data);
    	return $response->withData(['code'=>1]);
    }


	/**
     * @RequestMapping(route="select")
     * http://127.0.0.1:18306/wsq/select/
     * 数据库添加数据
     * swoftcli run -c http:start -b bin/swoft 自动重启服务
     */
    public function select(Response $response){
    	$id = 2;
    	// $data = Emp::find($id);
		// User::where('id', 1)->first(['id', 'name']);
		$data = Emp::find($id, ['id', 'ename']);
        // $data = UserInfo::find($id);
		// User::where('id', 1)->first(['id', 'name']);
    	return $response->withData(['code'=>1,'data'=>$data]);
    }

    /**
     * @RequestMapping(route="shiwu")
     * http://127.0.0.1:18306/wsq/shiwu/
     * 数据库事务的使用
     * swoftcli run -c http:start -b bin/swoft 自动重启服务
     */
    public function shiwu(Response $response)
    {
        try {
            DB::beginTransaction();//开启事物
            $_data = [
                'ename'=>'wsq',
            ];
            $id = '1';
            $_model = Emp::where(['id'=>$id])->update($_data);

            if(!$_model) throw new \Exception("更新emp失败", -1);
            
            $res_update = UserInfo::where(['id'=>1])->update(['email'=>'xmeme@qq.com']);

            if(!$res_update) throw new \Exception("更新UserInfo失败", -1);
            DB::commit();//提交事物
            return $response->withData(['code'=>1,'data'=>'success']);
        } catch (\Exception $e) {
            DB::rollBack();//回滚
            return $response->withData(['code'=>$e->getCode(),'data'=>$e->getMessage()]);
        }
    }

    /**
     * @RequestMapping(route="getrpcdata")
     * http://127.0.0.1:18306/wsq/getrpcdata/
     * RPC的调用
     * swoftcli run -c http:start -b bin/swoft 自动重启服务
     */
    public function getrpcdata(Response $response)
    {
        $user1 = request('tcp://127.0.0.1:18307', \App\Rpc\Lib\UserInterface::class, 'getList',  [1, 2], "1.2");
        $user2 = request('tcp://127.0.0.1:18307', \App\Rpc\Lib\UserInterface::class, 'getList',  [1, 2], "1.0");
        // $order = request('tcp://127.0.0.1:18307', \App\Rpc\Lib\OrderInterface::class, 'orderlist',  [10], "1.0");
        $order = request('tcp://127.0.0.1:18307', \App\Rpc\Lib\OrderInterface::class, 'sqldata',  ['xiaoming'], "1.0");
        return $response->withData(['code'=>1,'order'=>$order,'user1'=>$user1,'user2'=>$user2]);
    }

    /**
     * @RequestMapping(route="getredispool")
     * http://127.0.0.1:18306/wsq/getredispool/
     * redis的使用  连接池的方式
     * swoftcli run -c http:start -b bin/swoft 自动重启服务
     */
    public function getredispool(Response $response)
    {
        $key   = 'key';
        $value = uniqid();
        $this->redis->set($key, $value);
        $get = $this->redis->get($key);
        return $response->withData(['get'=>$get,'value'=>$value]);
    }


    /**
     * @RequestMapping(route="getredislink")
     * http://127.0.0.1:18306/wsq/getredislink/
     * redis的使用  普通链接方式
     * swoftcli run -c http:start -b bin/swoft 自动重启服务
     */
    public function getredislink(Response $response)
    {
       Redis::set('user:profile:', "Swoft33");
        $userDesc = Redis::get('user:profile:');
        return $response->withData(['data'=>$userDesc]);
    }


     /**
     * @RequestMapping(route="testexception")
     * http://127.0.0.1:18306/wsq/testexception/
     * 异常处理
     * swoftcli run -c http:start -b bin/swoft 自动重启服务
     */
    public function testexception(Response $response)
    {   
        throw new ApiException('为什么有异常89',-1);
    }

     /**
     * @RequestMapping(route="middles")
     * @Middleware(TestMiddleware::class)
     * http://127.0.0.1:18306/wsq/middles/
     * 中间件  测试
     * swoftcli run -c http:start -b bin/swoft 自动重启服务
     */
    public function middle(Response $response,Request $request)
    {
        return $response->withData(['user'=>$request->user]);
    }
}